package com.zyz.bookborrowing.service;

import com.zyz.bookborrowing.bean.Book;
import com.zyz.bookborrowing.bean.LendList;
import com.zyz.bookborrowing.bean.ReaderCard;
import com.zyz.bookborrowing.dao.ILendDao;
import com.zyz.bookborrowing.dao.LendDaoImpl;
import com.zyz.bookborrowing.utils.BookFlagE;

public class LendServiceImpl implements ILendService{

    ILendDao lendDao = new LendDaoImpl();
    IReaderCardService readerCardService = new ReaderCardServiceImpl();
    IBookService bookService = new BookServiceImpl();

    @Override
    public Integer borrowLendList(LendList lendList) {
//        更新借书卡的状态和书籍的状态
        ReaderCard readerCard = readerCardService.queryById(Integer.parseInt(lendList.getCardId()));
        readerCard.setState(1);
        readerCardService.updateReaderCard(readerCard);

        Book book = bookService.queryById(lendList.getBookId());
//        System.out.println(book);
        book.setState(BookFlagE.BORROW.code);
//        System.out.println(book);
        bookService.updateBook(book);

        return lendDao.borrowLendList(lendList);
    }

    @Override
    public Integer backLendList(LendList lendList) {
        //        更新借书卡的状态和书籍的状态
        ReaderCard readerCard = readerCardService.queryById(Integer.parseInt(lendList.getCardId()));
        readerCard.setState(0);
        readerCardService.updateReaderCard(readerCard);

        Book book = bookService.queryById(lendList.getBookId());
//        System.out.println(book);
        book.setState(BookFlagE.FREE.code);
//        System.out.println(book);
        bookService.updateBook(book);

        return lendDao.backLendList(lendList);
    }

    @Override
    public LendList queryByCardId(String cardId) {
        return lendDao.queryByCardId(cardId);
    }
}
